Unix Shell Scripting Interview Questions

Ratings:
(4)
Views: 115
Banner-Img
Share this blog:

 

1Q) What is UNIX?

Ans: It is a portable operating system that is designed for both efficient multi-tasking and multi-user functions. Its portability allows it to run on different hardware platforms. It was written in C and lets the user do processing and control under a shell.

2Q) What is Shell?

Ans: A shell acts as an interface between the user and the system. As a command interpreter, the shell takes commands and sets them up for execution.

3Q) What is Zombie process in UNIX? How do you find Zombie process in UNIX?

Ans: When a program forks and the child finishes before the parent, the kernel still keeps some of its information about

the child in case the parent might need it for example, the parent may need to check the child’s exit status. To be

able to get this information, the parent calls ‘wait()’; In the interval between the child terminating and the parent

calling ‘wait()’, the child is said to be a ‘zombie’ (If you do ‘ps’, the child will have a ‘Z’ in its status field to indicate this.)

4Q) What is nohup in UNIX?

Ans: Nohup is a special command which is used to run process in background, but it is slightly different than & which is normally used for putting a process in background. A UNIX process started with nohup will not stop even if the user who has stared log off from system. While background process started with & will stop as soon as user logoff.

5Q) What type of buffer is supported by Unix?

Ans:

  • Fully Buffered.
  • Line Buffered.
  • Un-Buffered.

6Q) What are signals?

Ans: Signals are software interrupts . Signal provides a way of handling asynchronous events: a user at a terminal typing the iterrupt key to stop a program or the next program in the pipeline termnating prematurely.

Interested in mastering Unix Shell Scripting Training? Enroll now for FREE demo on "Unix Shell Scripting Training"

7Q) What is the session?

Ans: A Session is a collection of one or more process group. A process establish a new session by calling setsid function. This function return process groupid ok.

8Q) What is ‘ps’ command for?

Ans: The ps command prints the process status for some or all of the running processes. The information given are the process identification number (PID),the amount of time that the process has taken to execute so far etc.

9Q) How are devices represented in UNIX?

Ans: All devices are represented by files called special files that are located in/dev directory. Thus, device files and other files are named and accessed in the same way. A ‘regular file’ is just an ordinary data file in the disk.

10Q) What are the different type of variables used in a Shell Script ?

Ans: In a shell script we can use two types of variables :

  • System-defined variables
  • User-defined variables

System-defined variables are defined or created by Operating System(Linux) itself. These variables are generally defined in Capital Letters and can be viewed by “set” command.

11Q) How to use Argument in a script?

Ans: First argument: $1,

Second argument : $2

Example : Script will copy file (arg1) to destination (arg2)

./copy.sh file1.txt /tmp/

cat copy.sh

#!/bin/bash

cp $1 $2

12Q) How to check if previous command run successful?

Ans: $?

13Q)How to check if file exist on filesystem?

Ans: if [ -f /var/log/messages ]

then

echo “File exists”

14Q) What it means #!/BIN/SH OR #!/BIN/BASH at beginning of every script?

Ans: That line tells which shell to use. #!/bin/bash script to execute using /bin/bash. In case of python script there there will be #!/usr/bin/python.

15Q) What is the first symbol in the bash script file?

Ans: #

16Q) What difference between & And && ?

Ans:

  • & – we using it when want to put script to background
  • && – when we want to execute command/script if first script was finished successfully.

17Q)What would be the output of command: ECHO ${NEW:-VARIABLE}?

Ans:

  • Variable

18Q) How to redirect STDOUT and STDERR Streams to LOG.TXT file from the script inside?

Ans: Add “exec >log.txt 2>&1” as the first command in the script.

19Q) How to get HOME_DIR with echo command only if string variable=”USER:123:321:/HOME/DIR” is given?

Ans:

echo ${variable#*:*:*:}

or

echo ${variable##*:}

20Q) How to list users which UID less than100(awk)?

Ans: awk -F: ‘$3<100’ /etc/passwd

21Q) How to change standard field separator to“:” in bash shell?

Ans: IFS=”:”

22Q) How to print all arguments provided to the script?

Ans: echo $*

or

echo $@

23Q)How to define array in bash?

Ans: array=(“Hi” “my” “name” “is”)

24Q) How will you pass and access arguments to a script in Linux?

Ans: Arguments can be passed as:

scriptName “Arg1” “Arg2”….”Argn” and can be accessed inside the script as $1 , $2 .. $n.

25Q) How do you write the contents of 3 files into a single file?

Ans: cat file1 file2 file3 > file

26Q) Write a command to find the sum of bytes (size of the file) of all files in a directory?

Ans: ls -l | grep '^-'| awk 'BEGIN {sum=0} {sum = sum + $5} END {print sum}'

27Q) Write a command to select only those lines containing "july" as a whole word?

Ans: grep -w july filename

The '-w' option makes the grep command to search for exact whole words. If the specified pattern is found in a string, then it is not considered as a whole word. For example: In the string "mikejulymak", the pattern "july" is found. However "july" is not a whole word in that string.

28Q) Write a command to duplicate each line in a file?

Ans: sed 'p' < filename

29Q) Write a command to list the files in '/usr' directory that start with 'ch' and then display the number of lines in each file?

Ans: wc -l /usr/ch*

Another way is

find /usr -name 'ch*' -type f -exec wc -l {} ;

30Q) Write a command to remove the prefix of the string ending with '/'?

Ans: The basename utility deletes any prefix ending in /. The usage is mentioned below:

basename /usr/local/bin/file

This will display the only file.

31Q) Write down syntax for all loops in shell scripting?

Ans:

for loop :

for i in $( ls );

do echo item: $i

done

while loop :

#!/bin/bash

COUNTER=0

while [ $COUNTER -lt 10 ]; do

echo The counter is

$COUNTER let COUNTER=COUNTER+1

done

until loop :

#!/bin/bash

COUNTER=20 until [ $COUNTER -lt 10 ]; do

echo COUNTER $COUNTER

let COUNTER-=1

done

32Q)How to get part of string variable with echo command only ?

Ans: echo ${variable:x:y}

x - start position

y - length

example:

variable="My name is Petras, and I am developer."

echo ${variable:11:6} # will display Petras

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox